package cn.learn.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * @author huangyezhan
 * @version 1.0
 * @date 2021年08月28日14:26
 */
public class IoUtils {

    public static String ioAll2String(InputStream inputStream) throws IOException {
        byte[] bytes = new byte[inputStream.available()];
        int    read  = inputStream.read(bytes);
        return new String(bytes, StandardCharsets.UTF_8);
    }

    public static String ioFlush2String(InputStream inputStream) throws IOException {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        byte[]                buffer = new byte[1024];
        int                   length;
        while ((length = inputStream.read(buffer)) != -1) {
            result.write(buffer, 0, length);
        }
        return result.toString(StandardCharsets.UTF_8.name());
    }

}
